package com.aiwei.tdjk.convert;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Created By yyj
 * Date: 2017/4/17
 * Time: 18:51
 */
public class DateConverter implements Converter<String,Date> {
    private static final List<String> formarts = new ArrayList<String>(4);
    static{
        formarts.add("yyyy-MM-dd");
        formarts.add("yyyy-MM-dd HH:mm:ss");
        formarts.add("HH:mm");
        formarts.add("yyyy-MM-dd HH:mm");
        formarts.add("HH:mm:ss");
     }
    @Override
    public Date convert(String source) {
       String value = source.trim();
       if("".equals(value)){
           return null;
       }
       if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
           return  parseDate(source,formarts.get(0));
       }else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
           return  parseDate(source,formarts.get(1));
       } else if (source.matches("^(\\d{1,2}):(\\d{1,2})$")){
           return  parseDate(source,formarts.get(2));
       }else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
           return  parseDate(source,formarts.get(3));
       }else if (source.matches("^\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
           return  parseDate(source,formarts.get(4));
       }else{
           throw new IllegalArgumentException("Invalid boolean value "+source+"");
       }
    }
    public Date parseDate(String dateStr,String format) {
        Date date = null;
        DateFormat dateFormat = new SimpleDateFormat(format);
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }
}
